package com.gouqi.mall.controller;

import com.gouqi.mall.domain.Message;
import com.gouqi.mall.domain.dto.MessageResult;
import com.gouqi.mall.service.MessageService;
import com.gouqi.mall.utils.URLUtils;
import io.swagger.annotations.*;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @time:2019-1-23
 * @author:小费
 * 发送消息填报求收购或者收货消息
 */
@CrossOrigin
@Api(basePath = "/api/v1", value = "发布消息", description = "发布消息")
@Component
@RestController
@RequestMapping(value = "/message")
public class MessageController extends BaseController {

    @Autowired
    @Qualifier("messageServiceImpl")
    private MessageService messageService;

    @ResponseBody
    @RequestMapping(value = "/add.do",method = RequestMethod.POST, produces = "application/json")
    @ApiOperation(value = "添加动态消息",notes = "添加枸杞发布求购/收购消息",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header",name = "token",value = "用户token",required = true,dataType = "String")
    })
    public String addMessage(@RequestHeader String token,@RequestBody @Valid Message message){
        JSONObject jsonObject = new JSONObject();
        System.out.println("消息内容："+message.toString());
        Integer result = messageService.addMessage(message);
        message.setId(result);
        if (result>0) {
            jsonObject.put("status",200);
            jsonObject.put("message","发布消息成功！");
            jsonObject.put("id",result);
        } else {
            jsonObject.put("status",404);
            jsonObject.put("message","发布消息失败！");
        }
        return jsonObject.toString();
    }

    @ResponseBody
    @RequestMapping(value = "/update.do",method = RequestMethod.PUT, produces = "application/json")
    @ApiOperation(value = "更新动态消息",notes = "添加枸杞发布求购/收购消息",httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header",name = "token",value = "用户token",required = true,dataType = "String")
    })
    public String updateMessage(@RequestHeader String token,@RequestBody @Valid Message message){
        JSONObject json = new JSONObject();
        System.out.println("消息内容："+message.toString());
        Integer updateResult = messageService.updateMessage(message);
        if (updateResult>0) {
            json.put("status",200);
            json.put("message","更新消息成功！");
            json.put("id",updateResult);
        } else {
            json.put("status",404);
            json.put("message","更新消息失败！");
        }
        return json.toString();
    }

    @ResponseBody
    @RequestMapping(value = "/delete.do",method = RequestMethod.DELETE, produces = "application/json")
    @ApiOperation(value = "更新动态消息",notes = "添加枸杞发布求购/收购消息",httpMethod = "DELETE")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header",name = "token",value = "用户token",required = true,dataType = "String"),
            @ApiImplicitParam(paramType = "query",name = "id",value = "消息记录id",required = true,dataType = "Integer")
    })
    public String deleteMessage(@RequestHeader String token,@RequestParam Integer id){
        JSONObject json = new JSONObject();
        System.out.println("删除消息内容id："+id);
        Integer deleteResult = messageService.deleteMessage(id);
        System.out.println("deleteResult:"+deleteResult);
        if (deleteResult>0) {
            json.put("status",200);
            json.put("message","删除消息成功！");
            json.put("id",deleteResult);
        } else {
            json.put("status",404);
            json.put("message","更新消息失败！");
        }
        return json.toString();
    }

    @ResponseBody
    @RequestMapping(value = "/getAllMessages.do",method = RequestMethod.GET, produces = "application/json")
    @ApiOperation(value = "获取所有求购收购信息",notes = "获取所有求购收购信息",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header",name = "token",value = "用户token",required = true,dataType = "String")
    })
    public String getAllMessages(HttpServletRequest request){
        JSONObject jsonObject = new JSONObject();
        List<MessageResult> result = messageService.getAllMessages();
        for (int i = 0; i < result.size() ; i++) {
            //查询出来的列表查信息
            System.out.println("第"+i+"条信息："+result.get(i).toString());
            //图片数组
            String imageStr = result.get(i).getImageUrl();
            //按日期上传的文件夹时间
            String folderTime = result.get(i).getCreateTime().substring(0,10);
            //获取协议ip和端口号
            String origin = URLUtils.getOrigin(request);
            
            //数据库存的图片
            String[] imageArr = imageStr.split(",");
            //新的图片地址数组
            String[] newArrImageUrl = new String[imageArr.length];
            String imageUrlStr = "";
            for (int j = 0; j < imageArr.length; j++) {
                imageUrlStr = origin + "/upload/" + folderTime + "/" + imageArr[j];
                newArrImageUrl[j] = imageUrlStr;
            }
            result.get(i).setImageList(newArrImageUrl);
        }
        if (result.size()>0) {
            jsonObject.put("status",200);
            jsonObject.put("message","查询成功！");
            jsonObject.put("result",result);
        } else {
            jsonObject.put("status",404);
            jsonObject.put("message","查询失败！");
            jsonObject.put("result",null);
        }
        return jsonObject.toString();
    }

    @ResponseBody
    @RequestMapping(value = "/getOneMessage/byId",method = RequestMethod.GET, produces = "application/json")
    @ApiOperation(value = "获取信息详情",notes = "获取查看的单个消息详情",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header",name = "token",value = "用户token",required = true,dataType = "String"),
            @ApiImplicitParam(paramType = "query",name = "id",value = "消息id",required = true,dataType = "Integer")
    })
    public String getDescMessages(@RequestParam(name = "id") Integer id, HttpServletRequest request){
        JSONObject jsonObject = new JSONObject();
        MessageResult result = messageService.getDescMessage(id);
        System.out.println("消息详情："+result.toString());
        //按日期上传的文件夹时间
        String folderTime = result.getCreateTime().substring(0,10);
        //图片数组
        String imageStr = result.getImageUrl();
        //数据库存的图片
        String[] imageArr = imageStr.split(",");
        //获取协议ip和端口号
        String origin = URLUtils.getOrigin(request);
        //新的图片地址数组
        String[] newArrImageUrl = new String[imageArr.length];
        String imageUrlStr = "";
        for (int j = 0; j < imageArr.length; j++) {
            imageUrlStr = origin + "/upload/" + folderTime + "/" + imageArr[j];
            newArrImageUrl[j] = imageUrlStr;
        }
        result.setImageList(newArrImageUrl);
        if (result != null) {
            jsonObject.put("status",200);
            jsonObject.put("message","查询成功！");
            jsonObject.put("result",result);
        } else {
            jsonObject.put("status",404);
            jsonObject.put("message","查询失败！");
            jsonObject.put("result",null);
        }
        return jsonObject.toString();
    }
}
